

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/favicon.jpg">
  <link rel="icon" href="/blog/img/favicon.jpg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="_SayUw">
  <meta name="keywords" content="html css js">
  
    <meta name="description" content="8 大数据类型  在 ES5 前，我们已知的数据类型有 6 种：  Number  String  Boolean  Undefined  Null  Object    而当 ES6 问世至今，又追加了 2 种：  Symbol  BigInt     按照数据类型划分又可以分为基本数据类型和引用数据类型。其中 Object 为引用数据类型，其余皆为基本数据类型。而 Object 是个大类，其中">
<meta property="og:type" content="article">
<meta property="og:title" content="【知识点 | JS】数据类型、变量检测与声明">
<meta property="og:url" content="https://sayuw.gitee.io/blog/2022/09/23/jsDataType/index.html">
<meta property="og:site_name" content="SayUw&#39;s Blog">
<meta property="og:description" content="8 大数据类型  在 ES5 前，我们已知的数据类型有 6 种：  Number  String  Boolean  Undefined  Null  Object    而当 ES6 问世至今，又追加了 2 种：  Symbol  BigInt     按照数据类型划分又可以分为基本数据类型和引用数据类型。其中 Object 为引用数据类型，其余皆为基本数据类型。而 Object 是个大类，其中">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-09-23T05:35:22.000Z">
<meta property="article:modified_time" content="2023-03-23T01:18:57.775Z">
<meta property="article:author" content="_SayUw">
<meta property="article:tag" content="js">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>【知识点 | JS】数据类型、变量检测与声明 - SayUw&#39;s Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"sayuw.gitee.io","root":"/blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":60,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":3},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>_SayUw</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/post.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="【知识点 | JS】数据类型、变量检测与声明"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-09-23 13:35" pubdate>
          2022年9月23日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.6k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          39 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">【知识点 | JS】数据类型、变量检测与声明</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="8 大数据类型"><a href="#8 大数据类型" class="headerlink" title="8 大数据类型"></a>8 大数据类型 </h2><p> 在 ES5 前，我们已知的数据类型有 6 种：</p>
<ul>
<li><p>Number</p>
</li>
<li><p>String</p>
</li>
<li><p>Boolean</p>
</li>
<li><p>Undefined</p>
</li>
<li><p>Null</p>
</li>
<li><p>Object</p>
</li>
</ul>
<p> 而当 ES6 问世至今，又追加了 2 种：</p>
<ul>
<li><p>Symbol</p>
</li>
<li><p>BigInt</p>
</li>
</ul>
<hr>
<p> 按照数据类型划分又可以分为基本数据类型和引用数据类型。其中 Object 为引用数据类型，其余皆为基本数据类型。而 Object 是个大类，其中包含 Function（函数）、Array（数组）、Date（日期）、RegExp（正则表达式）……</p>
<h3 id="基本数据类型和引用数据类型的区别"><a href="# 基本数据类型和引用数据类型的区别" class="headerlink" title="基本数据类型和引用数据类型的区别"></a> 基本数据类型和引用数据类型的区别 </h3><p> 基本数据类型（值类型）：在内存中占据固定大小，保存在栈内存中。基本数据类型复制后 2 个变量是独立的，因为是把值拷贝了一份。</p>
<p> 引用数据类型（复杂数据类型）：保存在堆内存中，栈内存存储的是对象的变量标识符以及对象在堆内存中的存储地址。引用数据类型则是复制了一个指针，2 个变量指向的值是该指针所指向的内容，一旦一方修改，另一方也会受到影响。</p>
<h3 id="Number"><a href="#Number" class="headerlink" title="Number"></a>Number</h3><p>JS 中的数字类型，包含整形和浮点型，也可以使用科学计数法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> num1 = <span class="hljs-number">1</span>;<br><span class="hljs-keyword">var</span> num2 = <span class="hljs-number">1.1</span>;<br><span class="hljs-keyword">var</span> num3 = <span class="hljs-number">1e7</span>;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num1); <span class="hljs-comment">// 1</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num2); <span class="hljs-comment">// 1.1</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num3); <span class="hljs-comment">// 10000000</span><br></code></pre></td></tr></table></figure>

<p> 其中包含了几种特殊数值：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> num1 = <span class="hljs-title class_">NaN</span>;<br><span class="hljs-keyword">var</span> num2 = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">pow</span>(<span class="hljs-number">2</span>, <span class="hljs-number">1024</span>);<br><span class="hljs-keyword">var</span> num3 = -<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">pow</span>(<span class="hljs-number">2</span>, <span class="hljs-number">1024</span>);<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num1); <span class="hljs-comment">// NaN</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num2); <span class="hljs-comment">// Infinity</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num3); <span class="hljs-comment">//-Infinity</span><br></code></pre></td></tr></table></figure>

<ul>
<li><p>NaN：NaN 是一个特殊的 Number，与其他所有值都不相等，包括它自身。可以用 isNaN () 去判断，是返回 true，否则返回 false。</p>
</li>
<li><p>Infinity：表示无穷，但其实是因为大于或等于 2 的 1024 次方的数值，JS 无法表示，所以统一显示为 Infinity</p>
</li>
</ul>
<blockquote>
<p>PS：JS 中所有数字都保存成 64 位浮点数，数值的精度只能到 53 个二进制位（相当于 16 个十进制位），大于这个范围的整数，JS 是无法精确表示的。</p>
<p> 例： <code>Math.pow (2, 53) === Math.pow (2, 53) + 1 //true</code></p>
</blockquote>
<h3 id="String"><a href="#String" class="headerlink" title="String"></a>String</h3><p> 字符串是存储字符的变量，可以使用单引号和双引号，字符串可以使用 <code>+</code> 进行拼接。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> str1 = <span class="hljs-string">&#x27;Hello&#x27;</span>;<br><span class="hljs-keyword">var</span> str2 = <span class="hljs-string">&quot;World&quot;</span>;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(str1 + str2); <span class="hljs-comment">// HelloWorld</span><br></code></pre></td></tr></table></figure>

<h3 id="Boolean"><a href="#Boolean" class="headerlink" title="Boolean"></a>Boolean</h3><p> 布尔值包含两个值，分别为：<code>true</code>、<code>false</code></p>
<h3 id="Undefined"><a href="#Undefined" class="headerlink" title="Undefined"></a>Undefined</h3><p> 根据字面意思，undefined 就是未定义的值，表示一个变量的一个最原始的状态。</p>
<ul>
<li><p> 变量声明但没有赋值时，其值为 undefined。</p>
</li>
<li><p> 函数没有返回值时，默认返回 undefined。</p>
</li>
<li><p> 访问对象中不存在的属性或未定义的变量，返回 undefined。</p>
</li>
<li><p> 调用函数时，没有传递实参，此时为 undefined。</p>
</li>
</ul>
<h3 id="Null"><a href="#Null" class="headerlink" title="Null"></a>Null</h3><p> 表示空值，在内存里表示为栈中的变量没有指向堆中的内存对象，一个对象可以是 null，代表空对象，null 本身也是对象。</p>
<ul>
<li><p> 原型链的最后终点为 null。</p>
</li>
<li><p> 当一个数据不再使用时，可以将其置为 null 释放引用。这一做法适用于大多数全局变量和全局对象的属性，局部变量会在它们离开执行环境时 (函数执行完时) 自动被解除引用。</p>
</li>
</ul>
<blockquote>
<p>PS：用一句话总结两者的区别就是：undefined 表示一个变量自然的、最原始的状态值，而 null 则表示一个变量被人为的设置为空对象，而不是原始状态。所以，在实际使用过程中，为了保证变量所代表的语义，不要对一个变量显式的赋值 undefined，当需要释放一个对象时，直接赋值为 null 即可。</p>
</blockquote>
<h3 id="Object"><a href="#Object" class="headerlink" title="Object"></a>Object</h3><p> 由花括号包括形成对象，对象的属性以键值对的方式定义，每个属性之间用逗号间隔声明。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> person = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;SayUw&#x27;</span>,<br>  <span class="hljs-attr">sex</span>: <span class="hljs-string">&#x27; 男 &#x27;</span>,<br>  <span class="hljs-attr">age</span>: <span class="hljs-number">23</span><br>&#125;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person); <span class="hljs-comment">// &#123;name: &#x27;SayUw&#x27;, sex: &#x27; 男 &#x27;, age: 23&#125;</span><br></code></pre></td></tr></table></figure>

<h3 id="Symbol"><a href="#Symbol" class="headerlink" title="Symbol"></a>Symbol</h3><p> 通过 Symbol () 生成的值是独一无二的值，就算使用相同的描述生成的 Symbol 也是不同的值。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> test1 = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">&#x27;test&#x27;</span>);<br><span class="hljs-keyword">var</span> test2 = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">&#x27;test&#x27;</span>);<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(test1 === test2); <span class="hljs-comment">//false</span><br></code></pre></td></tr></table></figure>

<p> 如果我们想生成两个相同的 Symbol 的话怎么办？官方为我们提供了 Symbol.for () 来实现这一效果。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> test1 = <span class="hljs-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-string">&#x27;test&#x27;</span>);<br><span class="hljs-keyword">var</span> test2 = <span class="hljs-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-string">&#x27;test&#x27;</span>);<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(test1 === test2); <span class="hljs-comment">//true</span><br></code></pre></td></tr></table></figure>

<p>Symbol.for () 方法会根据给定的值来从 symbol 的全局注册表中寻找对应的 symbol，如果找到则返回，没有找到则在全局注册表中新建一个对应的 symbol。</p>
<p> 当然，我们也可以用 Symbol.keyFor () 的方法反向获取到 symbol 变量的键值。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> test = <span class="hljs-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-string">&#x27;testKey&#x27;</span>);<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Symbol</span>.<span class="hljs-title function_">keyFor</span>(test)); <span class="hljs-comment">//testKey</span><br></code></pre></td></tr></table></figure>

<p> 通过这一特点，symbol 可以作为对象的唯一属性名，这样该属性就不会被覆盖或重写。并且通过该方式定义的属性，无法通过 for……in、Object.keys () 的方式访问到，只可以通过 Object.getOwnPropertySymbols () 的方式获取。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> obj = &#123;<br>  [<span class="hljs-title class_">Symbol</span>(<span class="hljs-string">&#x27;id&#x27;</span>)]: <span class="hljs-string">&#x27;symbol&#x27;</span>,<br>  <span class="hljs-attr">id</span>: <span class="hljs-string">&#x27;id&#x27;</span><br>&#125;<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">in</span> obj) &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); <span class="hljs-comment">//id</span><br>&#125;<br><br><span class="hljs-title class_">Object</span>.<span class="hljs-title function_">keys</span>(obj).<span class="hljs-title function_">map</span>(<span class="hljs-function"><span class="hljs-params">key</span> =&gt;</span> &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); <span class="hljs-comment">//id</span><br>&#125;)<br><br><span class="hljs-title class_">Object</span>.<span class="hljs-title function_">getOwnPropertySymbols</span>(obj).<span class="hljs-title function_">map</span>(<span class="hljs-function"><span class="hljs-params">key</span> =&gt;</span> &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); <span class="hljs-comment">// Symbol (id)</span><br>&#125;)<br></code></pre></td></tr></table></figure>

<h3 id="BigInt"><a href="#BigInt" class="headerlink" title="BigInt"></a>BigInt</h3><p>BigInt 是一种数字类型，只用来表示整数，没有位数限制，任何位数的整数都可以精确表示。为了与 Number 区分，BigInt 数据后面需要加 <strong>n</strong> 。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123</span>); <span class="hljs-comment">// 123</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123n</span>); <span class="hljs-comment">// 123n</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123n</span> + <span class="hljs-number">456n</span>); <span class="hljs-comment">// 579n</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123</span> + <span class="hljs-number">123n</span>); <span class="hljs-comment">// Cannot mix BigInt and other types</span><br></code></pre></td></tr></table></figure>

<h2 id="数据类型检测方法"><a href="# 数据类型检测方法" class="headerlink" title="数据类型检测方法"></a> 数据类型检测方法 </h2><h3 id="typeof"><a href="#typeof" class="headerlink" title="typeof"></a>typeof</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-number">1</span>);               <span class="hljs-comment">//number</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">true</span>);            <span class="hljs-comment">//boolean</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-string">&#x27;str&#x27;</span>);           <span class="hljs-comment">//string</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-title class_">Symbol</span>)           <span class="hljs-comment">//function</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;&#125;);    <span class="hljs-comment">//function</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>());   <span class="hljs-comment">//undefined</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> []);              <span class="hljs-comment">//object </span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> &#123;&#125;);              <span class="hljs-comment">//object</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">null</span>);            <span class="hljs-comment">//object</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">undefined</span>);       <span class="hljs-comment">//undefined</span><br></code></pre></td></tr></table></figure>

<p> 能够区分基本数据类型，但不能区分 Object、Array、Null。</p>
<h3 id="instanceof"><a href="#instanceof" class="headerlink" title="instanceof"></a>instanceof</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">1</span> <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Number</span>);                    <span class="hljs-comment">//false</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-literal">true</span> <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Boolean</span>);                <span class="hljs-comment">//false </span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;str&#x27;</span> <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">String</span>);                <span class="hljs-comment">//false  </span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>([] <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Array</span>);                    <span class="hljs-comment">//true</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;&#125; <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Function</span>);       <span class="hljs-comment">//true</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(&#123;&#125; <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Object</span>);                   <span class="hljs-comment">//true</span><br></code></pre></td></tr></table></figure>

<p> 能够区分 Object、Array、Function，但不能区分基本数据类型。</p>
<h3 id="Object-prototype-toString-call"><a href="#Object-prototype-toString-call" class="headerlink" title="Object.prototype.toString.call ()"></a>Object.prototype.toString.call ()</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> toString = <span class="hljs-title class_">Object</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">toString</span>;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(toString.<span class="hljs-title function_">call</span>(<span class="hljs-number">1</span>));                      <span class="hljs-comment">//[object Number]</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(toString.<span class="hljs-title function_">call</span>(<span class="hljs-literal">true</span>));                   <span class="hljs-comment">//[object Boolean]</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(toString.<span class="hljs-title function_">call</span>(<span class="hljs-string">&#x27;mc&#x27;</span>));                   <span class="hljs-comment">//[object String]</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(toString.<span class="hljs-title function_">call</span>([]));                     <span class="hljs-comment">//[object Array]</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(toString.<span class="hljs-title function_">call</span>(&#123;&#125;));                     <span class="hljs-comment">//[object Object]</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(toString.<span class="hljs-title function_">call</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;&#125;));           <span class="hljs-comment">//[object Function]</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(toString.<span class="hljs-title function_">call</span>(<span class="hljs-literal">undefined</span>));              <span class="hljs-comment">//[object Undefined]</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(toString.<span class="hljs-title function_">call</span>(<span class="hljs-literal">null</span>));                   <span class="hljs-comment">//[object Null]</span><br></code></pre></td></tr></table></figure>

<p> 能够区分基本数据类型和引用数据类型。</p>
<h2 id="变量声明"><a href="# 变量声明" class="headerlink" title="变量声明"></a> 变量声明 </h2><p>ES6 带来了 let 和 const 两种变量声明的关键字。</p>
<p> 三者有以下区别：</p>
<ul>
<li><p>var 声明的变量，可以跨块访问；<br>let 声明的变量，不可以跨块访问；<br>const 声明的变量，不可以跨块访问，且不能修改 </p>
</li>
<li><p>var 可以先使用后声明，存在变量提升，let 只能先声明后使用 </p>
</li>
<li><p>var 可以重复声明同一个变量，let 和 const 不行 </p>
</li>
</ul>
<h2 id="参考链接"><a href="# 参考链接" class="headerlink" title="参考链接"></a> 参考链接 </h2><p><a target="_blank" rel="noopener" href="https://juejin.cn/post/7000754813801775111">JavaScript 数据类型之 Symbol、BigInt - 掘金 </a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_33408245/article/details/82953143?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~Rate-5-82953143-blog-122847268.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~Rate-5-82953143-blog-122847268.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=6">js 中的 Symbol 数据类型_一止弋的博客 - CSDN 博客_js symbol</a></p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/libo-web/archive/2021/12/30/15746881.html">JS 中 8 种数据类型 - 不知名前端李小白 - 博客园 </a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_42239440/article/details/107812876">Undefined 与 Null 的区别_1planet 的博客 - CSDN 博客_null 和 undefined 的区别 </a></p>
<hr>
<p> 写在最后：写了半天发现最基本的没写，赶紧补上，刚好对于新增加的 Symbol 和 BigInt 不太熟悉，正好系统性的学习一下。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/%E7%9F%A5%E8%AF%86%E7%82%B9/" class="category-chain-item">知识点</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/js/">#js</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>【知识点 | JS】数据类型、变量检测与声明</div>
      <div>https://sayuw.gitee.io/blog/2022/09/23/jsDataType/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>_SayUw</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年9月23日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2022/10/26/BFC/" title="【知识点 | CSS】BFC">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">【知识点 | CSS】BFC</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2022/09/20/jsPrototype/" title="【知识点 | JS】原型和原型链">
                        <span class="hidden-mobile">【知识点 | JS】原型和原型链</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
      world.execute(me);
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
